package com.zztl.infiniteBand.ui.activity

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.andview.refreshview.XRefreshView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.ServiceMessageAdapter
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.model.BaseResponseBean
import com.zztl.infiniteBand.model.ServiceNoticeBean
import com.zztl.infiniteBand.model.ServiceNoticeDatamapBean
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.requestBean.MsgTypeRequest
import com.zztl.infiniteBand.presenter.MessageListPresenter
import com.zztl.infiniteBand.ui.view.mvpview.MessageListView
import kotlinx.android.synthetic.main.activity_record.*
import java.util.*

class MessageCenterActivity : BaseMvpActivity<MessageListView, MessageListPresenter>(), BaseQuickAdapter.OnItemClickListener, MessageListView {
    /*  @BindView(R.id.rv)
      lateinit  var rv: RecyclerView
      @BindView(R.id.xrefreshview)
      lateinit var xRefreshView: XRefreshView*/
/*    @BindView(R.id.ll_nodata)
    lateinit var ll_nodata: LinearLayout*/


    private lateinit var mServiceList: ArrayList<ServiceNoticeDatamapBean>
    private lateinit var mAdapter: ServiceMessageAdapter
    lateinit var mPresenter: MessageListPresenter
    private lateinit var mMsgTypeRequest: MsgTypeRequest

    override fun setLayoutId(): Int = R.layout.activity_message_center


    override fun initBase() {
        setTitle(getString(R.string.消息中心))
        mServiceList = ArrayList()
        rv.let {
            it.layoutManager=LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
            mAdapter = ServiceMessageAdapter(R.layout.item_message, mServiceList).apply {
                onItemClickListener = this@MessageCenterActivity
                it.adapter=this
            }
        }
        initRecycleview()
        mMsgTypeRequest = MsgTypeRequest(1, 5, "serviceNotice").apply {
            mPresenter.getMsgListByType(this, this@MessageCenterActivity)
        }

    }

    override fun createPresenter(): MessageListPresenter {
        mPresenter = MessageListPresenter()
        return mPresenter
    }


    private fun initRecycleview() {

        //设置刷新完成以后，headerview固定的时间
        xrefreshview?.let {
            it.setPinnedTime(800)
            it.setAutoLoadMore(false)
            it.setXRefreshViewListener(object : XRefreshView.SimpleXRefreshListener() {

                override fun onRefresh(isPullDown: Boolean) {
                    mMsgTypeRequest.page = 1
                    mPresenter.getMsgListByType(mMsgTypeRequest, this@MessageCenterActivity)
                }

                override fun onLoadMore(isSilence: Boolean) {
                    var page = mMsgTypeRequest.page
                    mMsgTypeRequest!!.page = ++page
                    mPresenter.getMsgListByType(mMsgTypeRequest, this@MessageCenterActivity)
                }
            })
        }

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

        val intent = Intent(this, TradingDetailActivity::class.java)
        intent.putExtra("datailData", mAdapter.data[position])
        showActivity(intent)
    }

    override fun failOrSuccess() {
        xrefreshview.stopRefresh()
        xrefreshview.stopLoadMore(true)
    }

    override fun getListSuccess(baseResponseBeanean: BaseResponseBean<*>) {
        val serviceNoticeBean = baseResponseBeanean as ServiceNoticeBean
        val size = serviceNoticeBean.data!!.rows!!.size
        val list = ArrayList<ServiceNoticeDatamapBean>()
        if (size > 0) {
            for (i in 0 until size) {
                val data = serviceNoticeBean.data!!.rows!![i].data
                val noticeDatamapBean = DataConverter.Companion.instance.getSingleBean(data!!, ServiceNoticeDatamapBean::class.java)
                list.add(noticeDatamapBean)
            }
        }
        if (list.size == 0 && mMsgTypeRequest.page == 1) {
            //todo  无数据
            xrefreshview.pullLoadEnable = false
            rv.visibility = View.GONE
            ll_nodata.visibility = View.VISIBLE
        } else {
            xrefreshview.pullLoadEnable = true
            rv.visibility = View.VISIBLE
            ll_nodata.visibility = View.GONE
            if (1 == mMsgTypeRequest.page) {
                mAdapter.setNewData(list)
            } else {
                mAdapter.addData(list)
            }
        }

    }

    override fun getListFail() {

    }
}
